package com.houdoudou.customer;

import java.util.ArrayList;  
import java.util.List;  

import com.houdoudou.customer.common.Constant;
import com.houdoudou.customer.common.ImageText;

import android.content.Context;  
import android.graphics.Color;  
import android.util.AttributeSet;  
import android.util.Log;  
import android.view.View;  
import android.widget.RelativeLayout;  

public class BottomControlPanel  extends RelativeLayout implements View.OnClickListener {

	 private Context mContext;  
	    private ImageText mBuyBtn = null;  
	    private ImageText mOrderBtn = null;  
	    private ImageText  mFindBtn = null;  
	    private ImageText mMeBtn = null;  
	    private int DEFALUT_BACKGROUND_COLOR = Color.rgb(243, 243, 243); //Color.rgb(192, 192, 192)  
	    private BottomPanelCallback mBottomCallback = null;  
	   
	    private List<ImageText> viewList = new ArrayList<ImageText>(); 
	    
	    public interface BottomPanelCallback{  
	        public void onBottomPanelClick(int itemId);  
	    }  
	    public BottomControlPanel(Context context, AttributeSet attrs) {  
	    	super(context, attrs);
	    	mContext=context;
	    	  
	        // TODO Auto-generated constructor stub  
	    }  
	    @Override  
	    protected void onFinishInflate() {  
	        // TODO Auto-generated method stub  
	        mBuyBtn = (ImageText)findViewById(R.id.btn_buy_act_main);  
	        mOrderBtn = (ImageText)findViewById(R.id.btn_order_act_main);  
	         mFindBtn = (ImageText)findViewById(R.id.btn_find_act_main);  
	        mMeBtn = (ImageText)findViewById(R.id.btn_me_act_main);  
	        setBackgroundColor(DEFALUT_BACKGROUND_COLOR);  
	        viewList.add(mBuyBtn);  
	        viewList.add(mOrderBtn);  
	        viewList.add( mFindBtn);  
	        viewList.add(mMeBtn);  
	     
	  
	    }  
	    public void initBottomPanel(){  
	        if(mBuyBtn != null){  
	            mBuyBtn.setImage(R.drawable.order_unselected);  
	            mBuyBtn.setText("购物");  
	        }  
	        if(mOrderBtn != null){  
	            mOrderBtn.setImage(R.drawable.buy_unselected);  
	            mOrderBtn.setText("订单");  
	        }  
	        if( mFindBtn != null){  
	             mFindBtn.setImage(R.drawable.find_unselected);  
	             mFindBtn.setText("发现");  
	        }  
	        if(mMeBtn != null){  
	            mMeBtn.setImage(R.drawable.me_unselected);  
	            mMeBtn.setText("我的");  
	        }  
	        setBtnListener();  
	  
	    }  
	    private void setBtnListener(){  
	        int num = this.getChildCount();  
	        for(int i = 0; i < num; i++){  
	            View v = getChildAt(i);  
	            if(v != null){  
	                v.setOnClickListener(this);  
	            }  
	        }  
	    }  
	    
	    public void setBottomCallback(BottomPanelCallback bottomCallback){  
	        mBottomCallback = bottomCallback;  
	    }  
	    @Override  
	    public void onClick(View v) {  
	        // TODO Auto-generated method stub  
	     /*   initBottomPanel();  //所有状态复位。
	        int index = -1;  
	        switch(v.getId()){  
	        case R.id.btn_message:  
	            index = Constant.BTN_FLAG_MESSAGE;  
	            mBuyBtn.setChecked(Constant.BTN_FLAG_MESSAGE);  
	            break;  
	        case R.id.btn_contacts:  
	            index = Constant.BTN_FLAG_CONTACTS;  
	            mOrderBtn.setChecked(Constant.BTN_FLAG_CONTACTS);  
	            break;  
	        case R.id.btn_news:  
	            index = Constant.BTN_FLAG_NEWS;  
	             mFindBtn.setChecked(Constant.BTN_FLAG_NEWS);  
	            break;  
	        case R.id.btn_setting:  
	            index = Constant.BTN_FLAG_SETTING;  
	            mMeBtn.setChecked(Constant.BTN_FLAG_SETTING);  
	            break;  
	        default:break;  
	        }  
	        if(mBottomCallback != null){  
	            mBottomCallback.onBottomPanelClick(index);  
	        }          
	                        不传这个值太麻烦，直接传ID回去。
	         */
	    	   initBottomPanel();  //所有状态复位。
	    	   int btnId=v.getId();
	    	   
	    	   mBuyBtn.setChecked(btnId);
	    	   mOrderBtn.setChecked(btnId);
	    	   mFindBtn.setChecked(btnId);
	    	   mMeBtn.setChecked(btnId);
	    //    System.out.println("------view id-------"+v.getId());
	        if(mBottomCallback != null){  
	            mBottomCallback.onBottomPanelClick(btnId);  
	        }
	    }  
	    
	    
	/*    public void defaultBtnChecked(){  
	        if(mBuyBtn != null){  
	            mBuyBtn.setChecked(Constant.BTN_FLAG_MESSAGE);  
	        }  
	    }   */
	    @Override  
	    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {  
	        // TODO Auto-generated method stub  
	        super.onLayout(changed, left, top, right, bottom);  
	        layoutItems(left, top, right, bottom);  
	    }  
	    
	    /**最左边和最右边的view由母布局的padding进行控制位置。这里需对第2、3个view的位置重新设置 
	     * @param left 
	     * @param top 
	     * @param right 
	     * @param bottom 
	     */  
	    private void layoutItems(int left, int top, int right, int bottom){  
	        int n = getChildCount();  
	        if(n == 0){  
	            return;  
	        }  
	        /*
	         * 原来是查询宽度，留间隔，这样单个控件就很小，我要让控件足够大。
	         * 
	        int paddingLeft = getPaddingLeft();  
	        int paddingRight = getPaddingRight();  
	        Log.i("yanguoqi", "paddingLeft = " + paddingLeft + " paddingRight = " + paddingRight);  
	        int width = right - left;  
	        int height = bottom - top;  
	        Log.i("yanguoqi", "width = " + width + " height = " + height);  
	        int allViewWidth = 0;  
	        for(int i = 0; i< n; i++){  
	            View v = getChildAt(i);  
	            Log.i("yanguoqi", "v.getWidth() = " + v.getWidth());  
	            allViewWidth += v.getWidth();  
	        }  
	        int blankWidth = (width - allViewWidth - paddingLeft - paddingRight) / (n - 1);  
	        Log.i("yanguoqi", "blankV = " + blankWidth );  
	  
	        LayoutParams params1 = (LayoutParams) viewList.get(1).getLayoutParams();  
	        params1.leftMargin = blankWidth;  
	        viewList.get(1).setLayoutParams(params1);  
	  
	        LayoutParams params2 = (LayoutParams) viewList.get(2).getLayoutParams();  
	        params2.leftMargin = blankWidth;  
	        viewList.get(2).setLayoutParams(params2);  
	        */
	        int paddingLeft = getPaddingLeft();  
	        int paddingRight = getPaddingRight();  
	        Log.i("yanguoqi", "paddingLeft = " + paddingLeft + " paddingRight = " + paddingRight);  
	        int width = right - left;  
	        int height = bottom - top;  
	        Log.i("yanguoqi", "width = " + width + " height = " + height);  
	        int allViewWidth = 0;  
	        for(int i = 0; i< n; i++){  
	            View v = getChildAt(i);  
	            Log.i("yanguoqi", "v.getWidth() = " + v.getWidth());  
	            allViewWidth += v.getWidth();  
	        }  
	        int blankWidth = (width - allViewWidth - paddingLeft - paddingRight) / (n - 1);  
	        Log.i("yanguoqi", "blankV = " + blankWidth );  
	  
	        LayoutParams params1 = (LayoutParams) viewList.get(1).getLayoutParams();  
	        params1.leftMargin = blankWidth;  
	        viewList.get(1).setLayoutParams(params1);  
	  
	        LayoutParams params2 = (LayoutParams) viewList.get(2).getLayoutParams();  
	        params2.leftMargin = blankWidth;  
	        viewList.get(2).setLayoutParams(params2);  
	    }  
	    
	    
}
